package org.hxyjs.dao.impl;

import org.hxyjs.dao.AdminRnoDao;
import org.hxyjs.entity.AdminRno;
import org.hxyjs.mapper.AdminRnoMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("adminRnoDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class AdminRnoDaoImpl implements AdminRnoDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private AdminRnoMapper adminRnoMapper;

    @Override
    public List<AdminRno> getAdminRnoByPage(PageInfo pageInfo) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", pageInfo.getCurrPage());
        map.put("pageSize", pageInfo.getPageSize());
        int itemCount = (int) adminRnoMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return adminRnoMapper.getAdminRnoByPage(map);
    }


    public List<AdminRno> getAllAdminRnos() throws Exception {
        return adminRnoMapper.selectByExample(null);
    }

    @Override
    public AdminRno getAdminRnoByRano(String rano) throws Exception {
        return adminRnoMapper.selectByPrimaryKey(rano);
    }


    @Override
    public Integer deleteAdminRnoByRano(String rano) {
        return adminRnoMapper.deleteByPrimaryKey(rano);
    }

    @Override
    public Integer updateAdminRnoByRano(AdminRno adminRno) {

        return adminRnoMapper.updateByPrimaryKey(adminRno);
    }

    @Override
    public Integer addAdminRno(AdminRno adminRno) {
        adminRno.setRano(initId());
        return adminRnoMapper.insertSelective(adminRno);
    }

    public String initId() {
        String maxRano = adminRnoMapper.getMaxRano();
        return GenNumTools.initId("6", 3, maxRano);
    }

}